using System;

namespace LinqRepositorySample.Domain
{
    public static class UnitOfWork
    {
        #region fields
        private static IUnitOfWork _currentUnitOfWork;
        #endregion

        #region properties
        public static IUnitOfWork Current
        {
            get { return _currentUnitOfWork; }
        }
        #endregion

        #region methods
        public static IUnitOfWork Start ()
        {
            if (_currentUnitOfWork != null)
                return _currentUnitOfWork;
            return _currentUnitOfWork = IoC.Container.Resolve<IUnitOfWork>();
        }

        public static void Finish (bool commit)
        {
            if (_currentUnitOfWork == null)
                throw new InvalidOperationException();
            if (commit)
                _currentUnitOfWork.CommitTransaction();
            _currentUnitOfWork.Dispose();
            _currentUnitOfWork = null;
        }
        #endregion
    }
}